﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {


        [OperationContract]
        List<Cards> Card_Get(Guid? clientid, string ckid, string goodcode, int? state, int ACID);

        [OperationContract]
        /// <summary>
        /// 空卡销售
        /// </summary>
        /// <param name="m">空卡销售主表</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        string KCSale(KCSale_M m, int ACID);

    }
    public partial class Service : IService
    {

        /// <summary>
        /// 取卡数据
        /// </summary>
        /// <param name="ckid">仓库ID</param>
        /// <param name="goodcode">产品编码</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public List<Cards> Card_Get(Guid? clientid,string ckid, string goodcode,int? state ,int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                Expression<Func<Cards, bool>> predicate = PredicateBuilder.True<Cards>();
                if (clientid!=null)
                {
                    predicate=p => p.ClientID == clientid;
                }
                else
                {
                    predicate.And(p => p.ClientID.HasValue==false);
                }
                if (!string.IsNullOrEmpty(ckid))
                {
                    predicate.And(p => p.WarehouseID == ckid);
                }
                if (!string.IsNullOrEmpty(goodcode))
                {
                    predicate.And(p => p.GoodsCode == goodcode);
                }
                if (state.HasValue)
                {
                    predicate.And(p => p.state == state);
                }
                return ctx.Cards.Where(predicate).ToList();
            }
        }

        /// <summary>
        /// 空卡销售
        /// </summary>
        /// <param name="m">空卡销售主表</param>
        /// <param name="ACID"></param>
        /// <returns></returns>
        public string KCSale(KCSale_M m,int ACID)
        {            
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        KCSale_M_Add(m, ACID, ctx);
                        foreach (KCSale_D d in m.KCSale_D)
                        {
                            KCSale_D_Add(d, ACID, ctx);
                            Cards card = ctx.Cards.Single(p => p.cno == d.cno);
                            card.ClientID = m.ClientID;
                            card.WarehouseID = "";

                        }
                        foreach (var kc in m.KCSale_D.GroupBy(p => p.GoodCode))
                        {
                            onhand o = ctx.onhand.Single(p => p.GoodsCode == kc.Key && p.WarehouseID == m.WarehouseID);

                            o.onhandcount -= kc.Count();
                        }
                        
                        RecordBalance(m.total, m.ClientID.Value, ctx, ACID);
                        ctx.SubmitChanges();
                        tc.Complete();
                        r = "";
                    }
                }
                catch(Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
            
           
        
        }

    }


}

